﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Compilation;
using System.Globalization;

namespace Wicresoft.SMS.Web
{
    public static class GlobalizationHelper
    { 
        public static string Resource(this HtmlHelper htmlHelper, string expression, params object[] args)
        {
            string path = "~/";
            ResourceExpressionFields fields = GetResourceFields(string.Format("Resources,{0}",expression), path);
                return GetGlobalResource(fields, args);
        }

        static string GetGlobalResource(ResourceExpressionFields fields, object[] args)
        {
            return string.Format((string)HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture), args);
        }

        static ResourceExpressionFields GetResourceFields(string expression, string virtualPath)
        {
            var context = new ExpressionBuilderContext(virtualPath);
            var builder = new ResourceExpressionBuilder();
            return (ResourceExpressionFields)builder.ParseExpression(expression, typeof(string), context);
        }
    }
}